package com.zhoug.ocr.textin;

import com.zhoug.common.utils.FileUtils;
import com.zhoug.common.utils.IOUtils;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.TimeUtils;

import java.io.File;
import java.nio.charset.StandardCharsets;

/**
 * @Author: zhoug
 * @Date: 2023/3/3
 * @Description:
 */
public class TextInUtils {
    private static final String TAG = ">>>Utils";

    /**
     * 保存日志到文件
     *
     * @param text
     */
    public static void outputBodyToFile(String text) {
        String externalFolder = FileUtils.getExternalFolder("0HttpResponse");
        if (externalFolder != null) {
            String path = new File(externalFolder, TimeUtils.getCurrentTime("yyyy-MM-dd_HH_mm_ss_SSS") + ".json").getAbsolutePath();
            boolean b = IOUtils.keepFile(path, text.getBytes(StandardCharsets.UTF_8), false);
            if (b) {
                LogUtils.d(TAG, "保存成功:" + path);
            } else {
                LogUtils.e(TAG, "保存失败:" + path);
            }
        }
    }

    /**
     * 去掉开头的冒号
     *
     * @param text
     * @return
     */
    public static String remoteStartColon(String text) {
        if (text != null && text.length() > 0) {
            if (text.startsWith(":")) {
                return text.replace(":", "").trim();
            } else if (text.startsWith("：")) {
                return text.replace("：", "").trim();
            }
            return text.trim();
        }

        return text;
    }

    /**
     * 年月日-> yyyy-mm-dd
     *
     * @param date
     * @return
     */
    public static String formatDate(String date, String separator) {
        if (date != null && date.length() > 0) {
//            Pattern compile = Pattern.compile("\\d{4}\\D*\\d{1,2}\\D*\\d{1,2}");
            date = date.trim().replaceAll("(\\d{4})\\D*(\\d{1,2})\\D*(\\d{1,2})\\D*", String.format("$1%s$2%s$3", separator, separator));
            String[] split = date.split(separator);
            int length = split.length - 1;
            if (length > 0) {
                StringBuilder result = new StringBuilder();
                for (int i = 0; i <= length; i++) {
                    String s = split[i];
                    if (s.length() == 1) {
                        result.append("0").append(s);
                    } else {
                        result.append(s);
                    }
                    if (i < length) {
                        result.append(separator);
                    }
                }
                date = result.toString();
            }
            return date;
        }
        return date;
    }

    public static String formatDate(String date) {
        return formatDate(date, "-");
    }

    /**
     * 7点21-8点30 ---> 07:21-08:30
     *
     * @param date
     * @param separator 开始时间和结束时间之前的分隔符 默认"-"
     * @return
     */
    public static String formatStartAndEndTime(String date, String separator) {
        try {
            String sp1 = ":";
            String sp2 = "-";
            if (separator != null) {
                sp2 = separator;
            }
            if (date != null && date.length() > 0) {
                date = date.trim().replaceAll("(\\d{1,2})\\D+(\\d{1,2})\\D+(\\d{1,2})\\D+(\\d{1,2})", String.format("$1%s$2%s$3%s$4", sp1, sp2, sp1));
                //07:21
                String[] split = date.split(sp2);
                StringBuilder result = new StringBuilder();
                int len1 = split.length - 1;
                for (int m = 0; m <= len1; m++) {
                    String item = split[m];
                    String[] split1 = item.split(sp1);
                    int length = split1.length - 1;
                    if (length > 0) {
                        for (int i = 0; i <= length; i++) {
                            String s = split1[i];
                            if (s.length() == 1) {
                                result.append("0").append(s);
                            } else {
                                result.append(s);
                            }
                            if (i < length) {
                                result.append(sp1);
                            }
                        }
                        if (m < len1) {
                            result.append(sp2);
                        }
                    }
                }
                date = result.toString();
                return date;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String formatStartAndEndTime(String date) {
        return formatStartAndEndTime(date,"-");
    }
}
